package com.molihub.utils;

/**
 * @ClassName: StringUtils
 * @Description: 字符串处理类
 * @author: GGBOY
 * @date 2020/2/16 20:31
 * @Version: 1.0
 **/
public class StringUtils {
    /**
     * 返回截取指定长度字节数后的字符串,多余部分用“...”代替
     *
     * @param strs 截取的字符串
     * @param length 截取的长度
     * @return java.lang.String
     * @author GGBOY
     * @date 2020/2/14
     */
    public static String cutStr(String strs, int length) {
        int sum = 0;
        String finalStr = "";
        if (null == strs || strs.getBytes().length <= length) {
            finalStr = (strs == null ? "" : strs);
        } else {
            for (int i = 0; i < strs.length(); i++) {
                String str = strs.substring(i, i + 1);
                // 累加单个字符字节数
                sum += str.getBytes().length;
                if (sum > length) {
                    finalStr = strs.substring(0, i) + "...";
                    break;
                }
            }
        }
        return finalStr;
    }
}
